package com.hwadeefsc.springboot.service.impl;

import com.hwadeefsc.springboot.dao.HomeworkDao;
import com.hwadeefsc.springboot.entity.Homework;
import com.hwadeefsc.springboot.service.HomeworkService;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import javax.annotation.Resource;

/**
 * (Homework)表服务实现类
 *
 * @author makejava
 * @since 2024-07-09 21:43:27
 */
@Service("homeworkService")
public class HomeworkServiceImpl implements HomeworkService {
    @Resource
    private HomeworkDao homeworkDao;

    /**
     * 通过ID查询单条数据
     *
     * @param homeworkId 主键
     * @return 实例对象
     */
    @Override
    public Homework queryById(Integer homeworkId) {
        return this.homeworkDao.queryById(homeworkId);
    }

    /**
     * 分页查询
     *
     * @param homework 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @Override
    public Page<Homework> queryByPage(Homework homework, PageRequest pageRequest) {
        long total = this.homeworkDao.count(homework);
        return new PageImpl<>(this.homeworkDao.queryAllByLimit(homework, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param homework 实例对象
     * @return 实例对象
     */
    @Override
    public Homework insert(Homework homework) {
        this.homeworkDao.insert(homework);
        return homework;
    }

    /**
     * 修改数据
     *
     * @param homework 实例对象
     * @return 实例对象
     */
    @Override
    public Homework update(Homework homework) {
        this.homeworkDao.update(homework);
        return this.queryById(homework.getHomeworkId());
    }

    /**
     * 通过主键删除数据
     *
     * @param homeworkId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer homeworkId) {
        return this.homeworkDao.deleteById(homeworkId) > 0;
    }
}
